package com.assa.ws.client.jaxws;


/**
 * 
 * WSN producer example: class to provide methods for creating a connection to
 * an application server, and for publishing XML file from a file to that
 * server.
 * @deprecated
 */
public class WSClientJAXWS {
	private static String SIMPLE_TOPIC_EXPRESSION = "http://docs.oasis-open.org/wsn/t-1/TopicExpression/Simple";
    private static String TOPIC_NS = "http://www.ibmcompany.com/wsn_topic_space";
    	
	public static void main(String[] args) {
		new WSClientJAXWS();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see java.lang.Object#Object()
	 */
	public WSClientJAXWS() {
		super();

		// get CBEs from file
		try {
			System.out.println(this.sendEvent());
		} catch (Exception e) {
			System.out.println("Exception: " + e.toString());
		}

	}

//	private CommonBaseEventType createNewCBE(String extensionName) {
//		CommonBaseEventType cbe = new CommonBaseEventType();
//
//		try {
//			XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
//			calendar.setYear(2009);
//			calendar.setMonth(9);
//			calendar.setDay(29);
//
//			cbe.setCreationTime(calendar);
//			cbe.setExtensionName(extensionName);
//
//			// The event bus only accepts events that start with a character
//			// (non-numeric),
//			// so we are ignoring al guid's until we are able to find one that meets
//			// this requirement
//
//			String guid;
//			char temp;
//			do {
//				UUID uuid = UUID.randomUUID();
//				guid = uuid.toString();
//				temp = guid.charAt(0);
//			} while (Character.isDigit(temp));
//
//			cbe.setGlobalInstanceId(guid);
//			cbe.setVersion("1.0.1");
//			cbe.setSequenceNumber(1L);
//
//			ComponentIdentificationType identificationType = new ComponentIdentificationType();
//			identificationType.setApplication("TestApplication");
//			identificationType.setComponent("TestComponent");
//			identificationType.setComponentIdType("TestComponentIdType");
//			identificationType.setLocation("TestLocation");
//			identificationType.setSubComponent("TestSubComponent");
//			identificationType.setLocationType("IPV4");
//			identificationType.setComponentType("TestComponentType");
//			cbe.setSourceComponentId(identificationType);
//
//			StartSituation startSituation = new StartSituation();
//			startSituation.setReasoningScope("EXTERNAL");
//			startSituation.setSuccessDisposition("SUCCESSFUL");
//			startSituation.setSituationQualifier("START_COMPLETED");
//
//			Situation situation = new Situation();
//			situation.setCategoryName("StartSituation");
//			situation.setSituationType(startSituation);
//
//			cbe.setSituation(situation);
//		} catch (DatatypeConfigurationException e) {
//			throw new RuntimeException(e);
//		}
//
//		return cbe;
//	}

	public String sendEvent() {
		String output = "Begin"; 
//		try {
//			String eventString = Constants.PROCESSSTART_EVENT;
//			eventString = eventString.replace(Constants.PROCESSSTART_LOANNUMBER, "1001");
//			eventString = eventString.replace(Constants.PROCESSSTART_LOANOFFICER, "Hernan Zapata");
//			eventString = eventString.replace(Constants.PROCESSSTART_DATESTART, "2009-09-29T11:00:00");
//			eventString = eventString.replace(Constants.PROCESSSTART_DATECOMPLETE, "2009-09-29T12:00:00");
//			CommonBaseEventType cbe = createNewCBE("mortgageLendingProcessStart");
//			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//			Document doc = builder.parse(new ByteArrayInputStream(eventString.getBytes("UTF-8")));
//			cbe.getAny().add(doc.getDocumentElement());
//			output = publishEvent(cbe);
//		} 
//		catch (Exception e) {
//			throw new RuntimeException(e);
//		}
		return output;
	}


//    public Element getCBERootElement(CommonBaseEventsType events)
//    {
//        MemoryStream memoryStream = new MemoryStream();
//        XmlSerializer serializer = new XmlSerializer(typeof(CommonBaseEventsType));
//        serializer.Serialize(memoryStream, events);
//        memoryStream.Position = 0;
//
//        XmlDocument eventsDoc = new XmlDocument();
//        eventsDoc.Load(memoryStream);
//
//        return eventsDoc.DocumentElement;
//
//    }
	
	
    private String publishEvent(CommonBaseEventType cbe)
    {
    	return null;
//    	try
//        {
//    		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//            Document xmlDocument = builder.newDocument();
//            /// Notification message that will be sent to the web service
//            Notify notification = new Notify();
//            NotificationMessageHolderType notificationHolder = new NotificationMessageHolderType();
//            /// Topic to which the notifications are sent
//            TopicExpressionType topic = new TopicExpressionType();
//            topic.setDialect(SIMPLE_TOPIC_EXPRESSION);
//            /// The follwing line specifies a namespace name, an attribute name and the namespace URI for an attribute
//            /// We are just interested in creating the topns namespace, so the attribute name can have any value
//            Attr attr = xmlDocument.createAttributeNS("dummy", TOPIC_NS);
//            attr.setPrefix("topns");
//            topic.getContent().add(attr);
//            Node textNode = xmlDocument.createTextNode("topns:cei_wsn_topic");
//            topic.getContent().add(textNode);
//
//            CommonBaseEventsType events = new CommonBaseEventsType();
//            events.getCommonBaseEvent().add(cbe);
//
//            /// Prepare the notification object that will be sent
//            notificationHolder.setTopic(topic);
//            NotificationMessageHolderType.Message message = new NotificationMessageHolderType.Message ();
//            message.setAny(events);
//            notificationHolder.setMessage(message);
//            notification.getNotificationMessage().add(notificationHolder);
//            
//    		// Create an instance of the generated proxy
//    		WSNServiceNotificationBroker serviceBroker = new WSNServiceNotificationBroker();
//    		NotificationBroker notificationBroker = serviceBroker.getWSNServicePointNotificationBrokerPort();
//            /// Make the service call to send the event
//    		notificationBroker.notify(notification);
//            return "Event was sent!!";
//        }
//        catch (Exception e)
//        {
//			throw new RuntimeException(e);
//		}
    }
	
	
	
	
	
	
	
	/**
	 * Publishes an array of CBE 1.0.1 objects as XML. Uses the
	 * NotificationBroker proxy generated from the WSDL exported by the server.
	 * 
	 * @param events
	 */

    
   
//    private void publishMessage(CommonBaseEventType events[]) {
//
//		String topic_ns = "http://www.ibmcompany.com/wsn_topic_space";
//
//		// Create an instance of the generated proxy
//		WSNServiceNotificationBroker serviceBroker = new WSNServiceNotificationBroker();
//		NotificationBroker notificationBroker = serviceBroker.getWSNServicePointNotificationBrokerPort();
//		try {
//
//			CommonBaseEventsType eventCollection = new CommonBaseEventsType();
//			eventCollection.
//
//			// Create the contents of the message
//			SOAPFactory soapFactory = SOAPFactory.newInstance();
//			SOAPElement messageContents = soapFactory.createElement("CommonBaseEvents");
//			messageContents.addNamespaceDeclaration("", "http://www.ibm.com/AC/commonbaseevent1_0_1");
//			messageContents.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
//			messageContents.setAttribute("xsi:schemaLocation", "http://www.ibm.com/AC/commonbaseevent1_0_1 commonbaseevent1_0_1.xsd");
//
//			setGUID(events, soapFactory, messageContents);
//
//			// Create a notification message from the contents
//			NotificationMessageHolderType message = new NotificationMessageHolderType();
//			message.setMessage((Message) messageContents);
//
//			// Constructor: TopicExpression(java.net.URI dialect,
//			// java.lang.String topic, java.util.Map prefixToNamespaceMappings
//			TopicExpressionType exp = new TopicExpressionType();
//			exp.setDialect(SIMPLE_TOPIC_EXPRESSION);
//			exp.getOtherAttributes().put(new QName("topns"), topic_ns);
//			exp.getContent().add("topns:cei_wsn_topic");
//
//			// Set the topic
//			message.setTopic(exp);
//
//			Notify notifyMessage = new Notify();
//			notifyMessage.getNotificationMessage().add(message);
//
//			notificationBroker.notify(notifyMessage);
//
//		} catch (Exception se) {
//			System.out.println("Exception: " + se.toString());
//		}
//	}
//
//	private void setGUID(CommonBaseEventType[] events, SOAPFactory soapFactory, SOAPElement messageContents) throws SOAPException {
//		for (int i = 0; i < events.length; i++) {
//			// update creation timestamps
//			long delta = System.currentTimeMillis() - events[i].getCreationTimeAsLong();
//			events[i].setCreationTimeAsLong(events[i].getCreationTimeAsLong() + delta);
//
//			// update the global instance ID
//			String globalInstanceId = (String) AccessController.doPrivileged(new PrivilegedAction() {
//				public Object run() {
//					return EventFactory.eINSTANCE.createGlobalInstanceId();
//				}
//			});
//			events[i].setGlobalInstanceId(globalInstanceId);
//
//			String foo = EventFormatter.toCanonicalXMLString(events[i], false);
//			// SOAPElement sElement = soapFactory.(foo);
//			// messageContents.addChildElement(sElement);
//
//		}
//	}
//
//	/*
//	 * Load CBEs from an xml file into objects of type
//	 * org.eclipse.hyades.logging.events.cbe.CommonBaseEvent
//	 */
//	public CommonBaseEventType[] createEvents() throws Exception {
//		CommonBaseEventType events[] = null;
//		File cbeFile = new File("c:\\temp\\events.xml");
//		if (cbeFile.canRead())
//			events = EventFormatter.eventsFromCanonicalXMLDoc(cbeFile);
//		else
//			System.out.println("Warning, could not load from file" + cbeFile.getName());
//		return events;
//	}
	
}